// let querykeyword=(window.location.search.split('='))[1];
// keyword=(decodeURIComponent(querykeyword));
// console.log(keyword);
let query = decodeURI(location.search);
let { keyword } = toObject(query);
$('#searchIn').val(keyword);


$('.search_keyword').children().eq(0).html(keyword);

let initData = {};
initData.keyword = keyword;
// initData.page=2
async function searchRender({ page = 1, keyword = '' }) {
    let [, result] = await myajax({
        url: '/api/search',
        data: {
            page,
            keyword
        }
    });
    // console.log(result);
    //渲染页面显示的课程
    let { page: page1, allPages: allPages1, totals, result: data } = result;
    $('.search_keyword').children().eq(1).html(totals);
    $('.search_keyword').children().eq(2).html(allPages1);
    let chtml = '';
    data.forEach((item) => {
        chtml += `<li cid="${item.cid}">
        <div class="top">
            <img src="${item.image_src}" alt="" class="m">
            <p>学科：${item.subject_name}</p>
        </div>
        <div class="bottom">
            <div class="left">
                <span>${item.title}</span>
                <span class="time">地区：${item.area_name}</span>
            </div>
            <div class="right">${item.price == 0 ? '免费学习' : '付费学习'}</div>
        </div>
    </li>`
    });


    $('.lesson ul').html(chtml);

    //分页渲染
    let prev = Number(page1) - 1 < 1 ? 1 : Number(page1) - 1;
    let next = Number(page1) + 1 > allPages1 ? allPages1 : Number(page1) + 1;
    let pagehtml = `<li val="${prev}"><a>上一页</a></li>`;
    for (var i = 1; i <= allPages1; i++) {
        pagehtml += `<li val="${i}"><a>${i}</a></li>`;
    }
    pagehtml += `<li val="${next}"><a>下一页</a></li>`

    $('#pageContainer').html(pagehtml);
    $('#pageContainer').children().eq(page1).children().addClass('active');
    $('#pageContainer').children().eq(page1).siblings().children().removeClass('active');
    if (page1 == 1) {
        $('#pageContainer').children().eq(0).children().addClass('hui');
    }
    if (page1 == allPages1) {
        $('#pageContainer').children().eq(allPages1 + 1).children().addClass('hui');
    }


}

searchRender(initData);

//页码的点击事件
$('#pageContainer').on('click','li',function(){
    let key=$(this).parent().attr('type');
    initData[key]=$(this).attr('val');
    searchRender(initData);
})

$('#searchBtn').click(function(){
    let keyword=$('#searchIn').val();
    window.location.href= `./pages/searchlist.html?keyword=${keyword}`;

})

//点击课程跳转到课程详情
$('.lesson ul').on('click','li',function(){
    let cid=$(this).attr('cid');
    location.href=`./pages/videoDetail.html?cid=${cid}`
})
    